<?php

class Static_page extends Controller {

	function Static_page(){
	
		parent::Controller();
        $this->load->helper(array('form','html'));
		
		$this->lang->load('recaptcha');
	
		$this->load->model(array(
		'User_model',
		'Dependency_model',
		'Your_info_model',
		'Location_model',
		'Home_info_model',
		'Political_party_model',
		'User_political_bias_model'
		));
		
		$this->load->library('pagination');
		$this->load->library('table');
		$this->load->library('form_validation');
	}
	
	function index()
	{		
	
		//$this->table->set_heading('Id', 'The Title', 'The Content');
		//$this->table->make_columns('Id', 1);
		
		
		$config['base_url'] = base_url().'/index.php/admin/users/index';
		$config['total_rows'] = $this->db->get('user_tiab')->num_rows();
		$config['per_page'] = 1;
		$config['num_links'] = 20;
		$config['full_tag_open'] = '<div class="pagination">';
		$config['full_tag_close'] = '</div>';
		
		$this->pagination->initialize($config);
		
		$this->db->select('firstname, city, created_date');
		
		$list = "edit";
		//$data['records'] = $this->table->make_columns($list, 1);
		$data['records'] = $this->db->get('user_tiab JOIN user_location', $config['per_page'], $this->uri->segment(4));
		


		$this->load->view(TEMPLATE_NAME.'/admin/static/about',$data);
	}
	
	function profile(){
	
	// data retrieving
		
		$id = $this->input->post('id');
		$username = $this->input->post('username');
		$firstname = $this->input->post('firstname');
		$surname = $this->input->post('surname');
		$email = $this->input->post('email');
		$password = $this->input->post('password');
		$confirm_password = $this->input->post('confirm_password');
		
		$real_pass = $password;
		$password = $this->User_model->hash_password($password);
		
            $config['upload_path']      = "./uploads/profile/real/";
            $config['allowed_types']    = 'gif|jpg|png|jpeg';
            $config['max_size']         = '2000';
            $config['max_width']        = '2000';
            $config['max_height']       = '2000';
            $config['encrypt_name']       = TRUE;
			
			$this->load->library('upload', $config);
	
	// $this->form_validation->set_rules('recaptcha_challenge_field', 'Captcha field', 'callback_check_captcha');
		
		$this->form_validation->set_rules('username', 'Username', 'required|min_length[6]|callback_check_username');
		$this->form_validation->set_rules('firstname', 'First name', 'required');
		$this->form_validation->set_rules('surname', 'Surname', 'required');
		$this->form_validation->set_rules('email', 'e-mail', 'required|valid_email|callback_check_email');
		
		$check_password = $this->input->post('password');
        $check_passconf = $this->input->post('confirm_password');
		
		if( !empty($check_password) ||  !empty($check_passconf)){
			$this->form_validation->set_rules('password', 'Password', 'required|matches[confirm_password]|min_length[6]');
			$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|min_length[6]');
		}
	
	//	end of $this->form_validation->set_rules
	
		$data['hidden_id']= $this->input->post('id');
		$data['user_list'] = $this->User_model->admin_list_user();
        $data['total_user'] = $this->User_model->total_user_tiab();
		$data['info']= $this->db->get('user_info');
		$data['info'] = $this->User_model->get_user_tiab($this->uri->segment(4));
		
		if($this->form_validation->run() == TRUE){
			//////////////////////////////
			
			if($_FILES['userfile']['tmp_name']){
                    if(!$this->upload->do_upload()){        
                        $data['error'] = $this->upload->display_errors();                
                    	$avatar = '';    
                    }else{
                        /* upload data*/                                
                        $avatar = $this->User_model->do_upload();            
                        //$this->upload->data();
                        //$data['upload_data'] = '1';
						//echo $avatar;
                    }
			}else{
				$avatar = '';
			}
			
			////////////////////////////////

			$succes_id = $this->User_model->update_user_tiab($id, $username, $firstname, $surname, $email, $real_pass, $password, $avatar);
			redirect('admin/users/profile/'.$id.'/success');
			//echo $avatar;
		}
		
		//echo "<pre>";print_r($data);echo "</pre>";
		//echo "<pre>";print_r($_FILES);echo "</pre>";
		//echo "<pre>";print_r($_POST);echo "</pre>";
		//echo "<pre>";print_r($test);echo "</pre>";
		$this->load->view(TEMPLATE_NAME.'/admin/user_profile',$data);
	}
		
	function info()
	{
		$data['user_list'] = $this->User_model->admin_list_user();
        $data['total_user'] = $this->User_model->total_user_tiab();			
		$this->db->where('id',$this->uri->segment(4));
		$data['query']= $this->db->get('user_tiab');
		$data['info']= $this->db->get('user_info');
		$data['info'] = $this->Your_info_model->get_your_info($this->uri->segment(4));
		$data['language'] = $this->Dependency_model->get_dependency('dep_language');
		$data['ethnic'] = $this->Dependency_model->get_dependency('dep_ethnic');
		$data['occupation'] = $this->Dependency_model->get_dependency('dep_occupation');		
		//echo "<pre>";print_r($data);echo "</pre>";
		$this->load->view(TEMPLATE_NAME.'/admin/user_info',$data);			
	}
	
	function location()
	{
		$data['user_list'] = $this->User_model->admin_list_user();
        $data['total_user'] = $this->User_model->total_user_tiab();			
		$this->db->where('id',$this->uri->segment(4));
		$data['query']= $this->db->get('user_tiab');		
		$data['info'] = $this->Location_model->get_location($this->uri->segment(4));
		//echo "<pre>";print_r($data);echo "</pre>";	
		$this->load->view(TEMPLATE_NAME.'/admin/user_location',$data);			
	}
	
	function homeinfo()
	{
		$data['user_list'] = $this->User_model->admin_list_user();
        $data['total_user'] = $this->User_model->total_user_tiab();			
		$this->db->where('id',$this->uri->segment(4));
		$data['query']= $this->db->get('user_tiab');
		$data['info'] = $this->Home_info_model->get_home_info($this->uri->segment(4));
		$data['income'] = $this->Dependency_model->get_dependency('dep_income');
		//echo "<pre>";print_r($data);echo "</pre>";
		$this->load->view(TEMPLATE_NAME.'/admin/user_homeinfo',$data);			
	}
	
	function politicalbias()
	{
		$data['user_list'] = $this->User_model->admin_list_user();
        $data['total_user'] = $this->User_model->total_user_tiab();				
		$this->db->where('id',$this->uri->segment(4));
		$data['query']= $this->db->get('user_tiab');
		$data['this_info'] = $this->Political_party_model->list_political_party();
		//$data['info'] = $this->User_political_bias_model->get_user_political_bias($this->uri->segment(4));
		//$data['info_pol_par'] = $this->Political_party_model->list_political_party();
		//echo "<pre>";print_r($data);echo "</pre>";
	
		$this->load->view(TEMPLATE_NAME.'/admin/user_politicalbias',$data);			
	}
	
	function networks()
	{
		$data['user_list'] = $this->User_model->admin_list_user();
        $data['total_user'] = $this->User_model->total_user_tiab();	
		//$data['hidden_id']= $this->input->post('id'); 		
		$this->db->where('id',$this->uri->segment(4));
		$data['query']= $this->db->get('user_tiab');
		$this->load->view(TEMPLATE_NAME.'/admin/user_networks',$data);			
	}
	
	function settings()
	{
		$data['user_list'] = $this->User_model->admin_list_user();
        $data['total_user'] = $this->User_model->total_user_tiab(); 		
		$this->db->where('id',$this->uri->segment(4));
		$data['query']= $this->db->get('user_tiab');
		$this->load->view(TEMPLATE_NAME.'/admin/user_settings',$data);			
	}
	
	function content()
	{
		$data['user_list'] = $this->User_model->admin_list_user();
        $data['total_user'] = $this->User_model->total_user_tiab();		
		$this->db->where('id',$this->uri->segment(4));
		$data['query']= $this->db->get('user_tiab');
		$this->load->view(TEMPLATE_NAME.'/admin/user_content',$data);			
	}
	
	function delete()
	{
		$data['user_list'] = $this->User_model->admin_list_user();
        $data['total_user'] = $this->User_model->total_user_tiab();		
		$this->db->where('id',$this->uri->segment(4));
		$data['query']= $this->db->get('user_tiab');
		$this->load->view(TEMPLATE_NAME.'/admin/user_delete',$data);			
	}
	
    function list_by_province(){
        $prov_name = $this->input->post('prov_name');
		$id_sort = "username";
		$sort_name = $this->input->post('sort_name');
        if($prov_name != 'all'){  
//admin_list_user($location = '', $order_condition = '', $order_field = '', $after = '', $order_direction = '', $search = '', $limit = '', $offset = '0')		
            //$data['user_list'] = $this->User_model->admin_list_user($prov_name,'',);
			$data['user_list'] = $this->User_model->admin_list_user($prov_name,'',$id_sort,'',$sort_name,'','','');
        }else{
            $data['user_list'] = $this->User_model->admin_list_user();                
        }
        echo json_encode($data);
    }
	
    function edit($user_tiab_id){
        $data = $this->User->get_user_tiab($user_tiab_id);
         /* initialize variable */
        $data['error'] = '';
        $data['upload_data'] = '';
        $data['user_tiab_id'] = $user_tiab_id;
        $this->load->view(TEMPLATE_NAME.'/admin/users/edit',$data);
    }
    
	 function check_email_json() {
        $email = $this->input->post('email');
        if(!$this->User_model->check_available_email($email)){            
            $result_email = 'FALSE';
        }else{
            $result_email = 'TRUE';
        }
        echo json_encode($result_email);
    }
    
    function check_username_json() {
        $username = $this->input->post('username');
        if(!$this->User_model->check_available_username($username)){            
            $result_username = 'FALSE';
        }else{
            $result_username = 'TRUE';
        }
        echo json_encode($result_username);
    }  
}

/* End of file users.php */
/* Location: ./system/application/controllers/users.php */
